package com.yida.gwj.config.service.impl;

import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.GwjConfCalculateType;
import com.yida.gwj.config.mapper.GwjConfCalculateTypeMapper;
import com.yida.gwj.config.service.IGwjConfCalculateTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 计算标准Service业务层处理
 *
 * @author lqq
 * @date 2022-12-13
 */
@Service
public class GwjConfCalculateTypeServiceImpl implements IGwjConfCalculateTypeService {

	@Autowired
	private GwjConfCalculateTypeMapper gcCalculateTypeMapper;

	@Override
	public List<GwjConfCalculateType> selectGwjConfCalculateTypeList(GwjConfCalculateType gcCalculateType) {
		return gcCalculateTypeMapper.selectGwjConfCalculateTypeList(gcCalculateType);
	}

	/**
	 * 查询计算标准列表
	 *
	 * @param calculateTypeId 计算标准主键
	 * @return 计算标准
	 */
	@Override
	public GwjConfCalculateType selectGwjConfCalculateTypeByCalculateTypeId(Long calculateTypeId) {
		return gcCalculateTypeMapper.selectGwjConfCalculateTypeByCalculateTypeId(calculateTypeId);
	}

	/**
	 * 新增计算标准
	 *
	 * @param gcCalculateType 计算标准
	 * @return 结果
	 */
	@Override
	public int insertGwjConfCalculateType(GwjConfCalculateType gcCalculateType) {
		checkGwjConfCalculateTypeByName(gcCalculateType.getName(), 0);
		gcCalculateType.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		gcCalculateType.setCreateTime(DateUtils.getNowDate());
		return gcCalculateTypeMapper.insertGwjConfCalculateType(gcCalculateType);
	}

	/**
	 * 修改计算标准
	 *
	 * @param gcCalculateType 计算标准
	 * @return 结果
	 */
	@Override
	public int updateGwjConfCalculateType(GwjConfCalculateType gcCalculateType) {
		checkGwjConfCalculateTypeByName(gcCalculateType.getName(), 1);
		gcCalculateType.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
		gcCalculateType.setUpdateTime(DateUtils.getNowDate());
		return gcCalculateTypeMapper.updateGwjConfCalculateType(gcCalculateType);
	}

	/**
	 * 20230112
	 * 
	 * @param name 标准名
	 */
	private void checkGwjConfCalculateTypeByName(String name, int type) {
		String typeInfo = "";
		if (0 == type) {
			typeInfo = "新增失败，";
		} else if (1 == type) {
			typeInfo = "更新失败，";
		}
		if (StringUtils.isEmpty(name)) {
			throw new ServiceException(typeInfo + "标准名称不能为空！");
		}
		if (gcCalculateTypeMapper.hasGwjConfCalculateTypeByName(name) > 0) {
			throw new ServiceException(typeInfo + "标准名称已存在！");
		}
	}

	/**
	 * 批量删除计算标准
	 *
	 * @param calculateTypeIds 需要删除的计算标准主键
	 * @return 结果
	 */
	@Override
	public int deleteByIds(Long[] calculateTypeIds) {
		List<GwjConfCalculateType> list = Arrays.stream(calculateTypeIds).map(id -> {
			GwjConfCalculateType temp = new GwjConfCalculateType();
			temp.setDelFlag(UserStatus.DELETED.getCode());
			temp.setCalculateTypeId(id);
			temp.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
			temp.setUpdateTime(DateUtils.getNowDate());
			return temp;
		}).collect(Collectors.toList());
		return gcCalculateTypeMapper.updateBatchGwjConfCalculateType(list);
	}

	/**
	 * 批量删除计算标准
	 *
	 * @param calculateTypeIds 需要删除的计算标准主键
	 * @return 结果
	 */
	@Override
	public int deleteGwjConfCalculateTypeByCalculateTypeIds(Long[] calculateTypeIds) {
		List<GwjConfCalculateType> list = Arrays.stream(calculateTypeIds).map(id -> {
			GwjConfCalculateType temp = new GwjConfCalculateType();
			temp.setDelFlag(UserStatus.DELETED.getCode());
			temp.setCalculateTypeId(id);
			temp.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
			temp.setUpdateTime(DateUtils.getNowDate());
			return temp;
		}).collect(Collectors.toList());
		return gcCalculateTypeMapper.updateBatchGwjConfCalculateType(list);
	}

}
